import urllib.parse
import urllib.request


# https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&start=0&limit=20


def create_request(page):
    url = 'https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&'
    data = {
        'start':(page-1)*20,
        'limit':20
    }
    data1 = urllib.parse.urlencode(data)
    url = url+data1
    print(url)
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0'}
    reques =  urllib.request.Request(url = url,headers = headers)
    return reques

def get_content(reques):
    open = urllib.request.urlopen(reques)
    com = open.read().decode('utf-8')
    print(com)
    return com

def down_load(page,com):
    fp = open('../爬取的文件/douban_'+str(page)+'.json','w',encoding='utf-8')

    fp.write(com)




if __name__=='__main__':
    start_page = int(input('请输入起始的页码'))
    end_page = int(input('请输入结束的页码'))

    for page in range(start_page,end_page+1):
        print(page)
        reques = create_request(page)
        com = get_content(reques)
        down_load(page,com)




